package com.j.lemon.learn;

import java.io.*;

/**
 * @Author lijunjun
 * @Date 2020-01-07 17:38
 * @Description
 */
public class MyClassLoader extends ClassLoader {

//    @Override
//    public Class<?> loadClass(String name) throws ClassNotFoundException {
//
//        try {
//            String n = name.substring(name.lastIndexOf(".") + 1) + ".class";
//            InputStream resourceAsStream = getClass().getResourceAsStream(n);
//
//            if (resourceAsStream == null) {
//                return super.loadClass(name);
//            } else {
//                byte[] b = new byte[resourceAsStream.available()];
//                resourceAsStream.read(b);
//                return defineClass(name, b, 0, b.length);
//            }
//        } catch (IOException e) {
//            throw new ClassNotFoundException(name);
//        }
//    }

    @Override
    protected Class<?> findClass(String name) {
        try {
            File file = new File("/Users/lijunjun/Desktop/"+name.substring(name.lastIndexOf(".")+1)+".class");
            InputStream inputStream = new FileInputStream(file);
            byte [] b = new byte[inputStream.available()];
            inputStream.read(b);
            return defineClass(name,b,0,b.length);
        } catch (IOException e) {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
////        MyClassLoader classLoader =  new MyClassLoader();
////        Class<?> sss = classLoader.loadClass("com.j.lemon.learn.T");
////        Object o = sss.newInstance();
////
////        System.out.println(o instanceof com.j.lemon.learn.Test);
//        System.out.println(sss.getClassLoader());
        TestDemo testDemo = new TestDemo();
        testDemo.aaa();
    }
}
